1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *   - Redistributions of source code must retain the above copyright
9    *     notice, this list of conditions and the following disclaimer.
10   *
11   *   - Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in the
13   *     documentation and/or other materials provided with the distribution.
14   *
15   *   - Neither the name of Oracle nor the names of its
16   *     contributors may be used to endorse or promote products derived
17   *     from this software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  
33  import java.util.EventObject;
34  import java.util.List;
35  import javax.swing.JTable;
36  import javax.swing.table.DefaultTableModel;
37  import javax.swing.table.TableCellEditor;
38  import javax.swing.table.TableCellRenderer;
39  import javax.swing.table.TableColumn;
40  
41  
42  /**
43   *  The OldJTable is an unsupported class containing some methods that were
44   *  deleted from the JTable between releases 0.6 and 0.7
45   */
46  @SuppressWarnings("serial")
47  public class OldJTable extends JTable
48  {
49     /*
50      *  A new convenience method returning the index of the column in the
51      *  co-ordinate space of the view.
52      */
53      public int getColumnIndex(Object identifier) {
54          return getColumnModel().getColumnIndex(identifier);
55      }
56  
57  //
58  //  Methods deleted from the JTable because they only work with the
59  //  DefaultTableModel.
60  //
61  
62      public TableColumn addColumn(Object columnIdentifier, int width) {
63          return addColumn(columnIdentifier, width, null, null, null);
64      }
65  
66      public TableColumn addColumn(Object columnIdentifier, List columnData) {
67          return addColumn(columnIdentifier, -1, null, null, columnData);
68      }
69  
70      // Override the new JTable implementation - it will not add a column to the
71      // DefaultTableModel.
72      public TableColumn addColumn(Object columnIdentifier, int width,
73                                   TableCellRenderer renderer,
74                                   TableCellEditor editor) {
75          return addColumn(columnIdentifier, width, renderer, editor, null);
76      }
77  
78      public TableColumn addColumn(Object columnIdentifier, int width,
79                                   TableCellRenderer renderer,
80                                   TableCellEditor editor, List columnData) {
81          checkDefaultTableModel();
82  
83          // Set up the model side first
84          DefaultTableModel m = (DefaultTableModel)getModel();
85          m.addColumn(columnIdentifier, columnData.toArray());
86  
87          // The column will have been added to the end, so the index of the
88          // column in the model is the last element.
89          TableColumn newColumn = new TableColumn(
90                  m.getColumnCount()-1, width, renderer, editor);
91          super.addColumn(newColumn);
92          return newColumn;
93      }
94  
95      // Not possilble to make this work the same way ... change it so that
96      // it does not delete columns from the model.
97      public void removeColumn(Object columnIdentifier) {
98          super.removeColumn(getColumn(columnIdentifier));
99      }
100 
101     public void addRow(Object[] rowData) {
102         checkDefaultTableModel();
103         ((DefaultTableModel)getModel()).addRow(rowData);
104     }
105 
106     public void addRow(List rowData) {
107         checkDefaultTableModel();
108         ((DefaultTableModel)getModel()).addRow(rowData.toArray());
109     }
110 
111     public void removeRow(int rowIndex) {
112         checkDefaultTableModel();
113         ((DefaultTableModel)getModel()).removeRow(rowIndex);
114     }
115 
116     public void moveRow(int startIndex, int endIndex, int toIndex) {
117         checkDefaultTableModel();
118         ((DefaultTableModel)getModel()).moveRow(startIndex, endIndex, toIndex);
119     }
120 
121     public void insertRow(int rowIndex, Object[] rowData) {
122         checkDefaultTableModel();
123         ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData);
124     }
125 
126     public void insertRow(int rowIndex, List rowData) {
127         checkDefaultTableModel();
128         ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData.toArray());
129     }
130 
131     public void setNumRows(int newSize) {
132         checkDefaultTableModel();
133         ((DefaultTableModel)getModel()).setNumRows(newSize);
134     }
135 
136     public void setDataVector(Object[][] newData, List columnIds) {
137         checkDefaultTableModel();
138         ((DefaultTableModel)getModel()).setDataVector(
139                 newData, columnIds.toArray());
140     }
141 
142     public void setDataVector(Object[][] newData, Object[] columnIds) {
143         checkDefaultTableModel();
144         ((DefaultTableModel)getModel()).setDataVector(newData, columnIds);
145     }
146 
147     protected void checkDefaultTableModel() {
148         if(!(dataModel instanceof DefaultTableModel))
149             throw new InternalError("In order to use this method, the data model must be an instance of DefaultTableModel.");
150     }
151 
152 //
153 //  Methods removed from JTable in the move from identifiers to ints.
154 //
155 
156     public Object getValueAt(Object columnIdentifier, int rowIndex) {
157         return super.getValueAt(rowIndex, getColumnIndex(columnIdentifier));
158     }
159 
160     public boolean isCellEditable(Object columnIdentifier, int rowIndex) {
161         return super.isCellEditable(rowIndex, getColumnIndex(columnIdentifier));
162     }
163 
164     public void setValueAt(Object aValue, Object columnIdentifier, int rowIndex) {
165         super.setValueAt(aValue, rowIndex, getColumnIndex(columnIdentifier));
166     }
167 
168     public boolean editColumnRow(Object identifier, int row) {
169         return super.editCellAt(row, getColumnIndex(identifier));
170     }
171 
172     public void moveColumn(Object columnIdentifier, Object targetColumnIdentifier) {
173         moveColumn(getColumnIndex(columnIdentifier),
174                    getColumnIndex(targetColumnIdentifier));
175     }
176 
177     public boolean isColumnSelected(Object identifier) {
178         return isColumnSelected(getColumnIndex(identifier));
179     }
180 
181     public TableColumn addColumn(int modelColumn, int width) {
182         return addColumn(modelColumn, width, null, null);
183     }
184 
185     public TableColumn addColumn(int modelColumn) {
186         return addColumn(modelColumn, 75, null, null);
187     }
188 
189     /**
190      *  Creates a new column with <I>modelColumn</I>, <I>width</I>,
191      *  <I>renderer</I>, and <I>editor</I> and adds it to the end of
192      *  the JTable's array of columns. This method also retrieves the
193      *  name of the column using the model's <I>getColumnName(modelColumn)</I>
194      *  method, and sets the both the header value and the identifier
195      *  for this TableColumn accordingly.
196      *  <p>
197      *  The <I>modelColumn</I> is the index of the column in the model which
198      *  will supply the data for this column in the table. This, like the
199      *  <I>columnIdentifier</I> in previous releases, does not change as the
200      *  columns are moved in the view.
201      *  <p>
202      *  For the rest of the JTable API, and all of its associated classes,
203      *  columns are referred to in the co-ordinate system of the view, the
204      *  index of the column in the model is kept inside the TableColumn
205      *  and is used only to retrieve the information from the appropraite
206      *  column in the model.
207      *  <p>
208      *
209      *  @param  modelColumn     The index of the column in the model
210      *  @param  width           The new column's width.  Or -1 to use
211      *                          the default width
212      *  @param  renderer        The renderer used with the new column.
213      *                          Or null to use the default renderer.
214      *  @param  editor          The editor used with the new column.
215      *                          Or null to use the default editor.
216      */
217     public TableColumn addColumn(int modelColumn, int width,
218                                  TableCellRenderer renderer,
219                                  TableCellEditor editor) {
220         TableColumn newColumn = new TableColumn(
221                 modelColumn, width, renderer, editor);
222         addColumn(newColumn);
223         return newColumn;
224     }
225 
226 //
227 //  Methods that had their arguments switched.
228 //
229 
230 // These won't work with the new table package.
231 
232 /*
233     public Object getValueAt(int columnIndex, int rowIndex) {
234         return super.getValueAt(rowIndex, columnIndex);
235     }
236 
237     public boolean isCellEditable(int columnIndex, int rowIndex) {
238         return super.isCellEditable(rowIndex, columnIndex);
239     }
240 
241     public void setValueAt(Object aValue, int columnIndex, int rowIndex) {
242         super.setValueAt(aValue, rowIndex, columnIndex);
243     }
244 */
245 
246     public boolean editColumnRow(int columnIndex, int rowIndex) {
247         return super.editCellAt(rowIndex, columnIndex);
248     }
249 
250     public boolean editColumnRow(int columnIndex, int rowIndex, EventObject e){
251         return super.editCellAt(rowIndex, columnIndex, e);
252     }
253 
254 
255 }  // End Of Class OldJTable